package com.ch999.detect.View.activity;

import android.graphics.Point;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.ch999.detect.AppBaseActivity;
import com.ch999.detect.R;
import com.ch999.detect.View.widget.camera.CameraInterface;
import com.ch999.detect.View.widget.camera.CameraSurfaceView;
import com.ch999.detect.View.widget.camera.DisplayUtil;
import com.ch999.detect.utils.CameraUtils;
import com.ch999.detect.utils.DeviceUtils;
import com.scorpio.mylib.ottoBusProvider.BusEvent;
import com.scorpio.mylib.ottoBusProvider.BusProvider;


public class CommerActivity extends AppBaseActivity implements CameraInterface.CamOpenOverCallback, View.OnClickListener {

    CameraSurfaceView surfaceView = null;

    public static boolean isBackCamerea = false;// true打开后置摄像头  false //打开前置摄像头

    public static String EXTRA_ISCAMEREA = "extra_iscamerea";

    float previewRate = -1f;

    TextView tv_call;
    TextView tv_true;
    TextView tv_false;

    int preGood, backGood;//前置、后置是否清晰
    boolean isFast;
    private boolean mIsOnlyFront;

    @Override
    public int getLayoutId() {
        return R.layout.activity_commer;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (!isFast) {
                    isFast = true;
                    openCamera();
                }
            }
        }).start();
        initViewParams();
    }

    private void openCamera() {

        new Thread() {
            @Override
            public void run() {

                boolean booleanExtra = getIntent().getBooleanExtra(EXTRA_ISCAMEREA, false);

                if (CameraUtils.HasFrontCamera() == 2 && CameraUtils.HasBackCamera() == 2) {
                    sendSuccess("0");
                    return;
                }

                if (CameraUtils.HasFrontCamera() == 2) {
                    isBackCamerea = true;
                }

                if (CameraUtils.HasFrontCamera() != 2 && CameraUtils.HasBackCamera() == 2) {
                    mIsOnlyFront = true;
                }

                if (isBackCamerea) {

                    if (DeviceUtils.hasBackFacingCamera()) {
                        CameraInterface.getInstance().doOpenCamera(CommerActivity.this, 0);
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(mContext, "无后置摄像头", Toast.LENGTH_SHORT).show();
                            }
                        });

                    }

                } else {

                    if (DeviceUtils.hasFrontFacingCamera()) {

                        CameraInterface.getInstance().doOpenCamera(CommerActivity.this, 1);

                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(mContext, "无前置摄像头", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }

            }
        }.start();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        CameraInterface.getInstance().doStopCamera();
        openCamera();
    }

    @Override
    public void cameraHasOpened() {
        SurfaceHolder holder = surfaceView.getSurfaceHolder();
        CameraInterface.getInstance().doStartPreview(this, holder, previewRate);
    }

    private void initViewParams() {
        ViewGroup.LayoutParams params = surfaceView.getLayoutParams();
        Point p = DisplayUtil.getScreenMetrics(this);
        params.width = p.x;
        params.height = (int) (p.x * 1.78);
        previewRate = DisplayUtil.getScreenRate(this); //默认全屏的比例预览
        surfaceView.setLayoutParams(params);

        //手动设置拍照ImageButton的大小为120dip×120dip,原图片大小是64×64
/*        ViewGroup.LayoutParams p2 = shutterBtn.getLayoutParams();
        p2.width = DisplayUtil.dip2px(this, 80);
        p2.height = DisplayUtil.dip2px(this, 80);;
        shutterBtn.setLayoutParams(p2);*/

    }

    @Override
    public void findView() {
        surfaceView = (CameraSurfaceView) findViewById(R.id.camera_surfaceview);
        tv_call = (TextView) findViewById(R.id.tv_call);
        tv_true = (TextView) findViewById(R.id.tv_true);
        tv_false = (TextView) findViewById(R.id.tv_false);
        tv_true.setOnClickListener(this);
        tv_false.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int vid = view.getId();
        if (vid == R.id.tv_true) {
            //清晰
            next(0);
        } else if (vid == R.id.tv_false) {
            //不清晰
            next(1);
        }
    }

    public void next(int i) {
        if (mIsOnlyFront) {
            isBackCamerea = true;
        }
        if (isBackCamerea) {
            backGood = i;


            if (preGood == 0 && backGood == 0) {
                //两个摄像头都清晰才为正常，否则不正常
                sendSuccess("0");
            } else {
                sendSuccess("1");
            }

            isBackCamerea = false;
            //结束

        } else {
            preGood = i;
            //打开后置摄像头
            tv_call.setText("摄像功能-后置摄像头");
//            CameraInterface.getInstance().doOpenCamera(CommerActivity.this, 0);
            isBackCamerea = true;
            CameraInterface.getInstance().doStopCamera();
            openCamera();
        }
    }

    private void sendSuccess(String i) {
        BusEvent event = new BusEvent();
        event.setAction(NewDetectActivity.BUSACTION_CAMERADETECT_SUCCESS);

        event.setContent(i);

        BusProvider.getInstance().post(event);
        finish();
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        isBackCamerea = true;
        next(1);
    }

    @Override
    protected void onPause() {
        super.onPause();
        CameraInterface.getInstance().doStopCamera();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
